# load packages
library(tidyverse)

all_fits <- readRDS("output/fits/_all-fits.rds") %>%
  #rename(model_code = model_id)   %>%
  #select(model_code, policy, issue, category, looic) %>%
  glimpse()

loos <- all_fits %>%
  select(model_code, looic, max_rhat)

qplot(x = max_rhat, y = model_code, data = all_fits)


gg_data <- all_fits %>%
  mutate(model_code = str_replace_all(model_code, "-", " : "),
         model_code = reorder(model_code, -looic)) %>%
  mutate(policy_fct = case_when(policy == 0 ~ "No parameters vary.",
                                policy == 1 ~ "Only the intercept varies.",
                                policy == 2 ~ "Both the intercept and the treatment effect vary"),
         policy_fct = reorder(policy_fct, policy)) %>%
  glimpse()

ggplot(gg_data, aes(y = model_code, x = looic, color = policy_fct)) + 
  geom_segment(xend = min(gg_data$looic, na.rm = TRUE), aes(yend = model_code), size = 0.2) + 
  geom_point(size = 0.2) + 
  theme_bw() + 
  theme(panel.grid = element_blank()) + 
  scale_color_manual(values = c("#7570b3", "#1b9e77", "#d95f02")) + 
  labs(title = "Comparison of the Model fit for Various Random Effect Structures",
       x = "Approximate LOO-CV",
       y = "Model Structure Code", 
       color = "Policy-Level Random Effect Structure") + 
  theme(legend.position = "bottom") + 
  guides(color = guide_legend(title.position = "top"))
ggsave("figs/figa2-model-fits.tiff", height = 8, width = 8)


